package com.xintonghua.bussiness.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.AddListProjectAdapter;
import com.xintonghua.bussiness.adapter.AddListProjectTypeAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.AddProjectBean;
import com.xintonghua.bussiness.bean.AddProjectTypeBean;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;

/**
 * 添加项目
 * Created by zhangxuemei on 2018/3/11.
 */

public class AddProjectActivity extends BaseActivity {
    @BindView(R.id.lv_type)
    ListView lvType;
    @BindView(R.id.lv_project)
    ListView lvProject;

    private AddListProjectAdapter mAdapter;
    private AddListProjectTypeAdapter mTypeAdapter;
    String type = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_add_project);
        ButterKnife.bind(this);

        type = JumpUtils.getString(this, "type");
        if (TextUtils.isEmpty(type)) {
            type = "";
        }
        initUI();
        requestTypeList();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();

        if (type.equals("1")) {
            setSimpleActionBar("项目价目表");
        } else {
            setSimpleActionBar("添加项目");
        }
        mTypeAdapter = new AddListProjectTypeAdapter(this, new ArrayList<AddProjectTypeBean>());
        lvType.setAdapter(mTypeAdapter);
        mAdapter = new AddListProjectAdapter(this, new ArrayList<AddProjectBean>());
        lvProject.setAdapter(mAdapter);
    }

    private void requestTypeList() {
        HttpCent.getInstance(this).shopProjectType(AuthManager.geteAuth().getShop_id(), this, MyState.requestOne);
    }

    private void requestProjectList(int projectId) {
        HttpCent.getInstance(this).shopProjectList(projectId, this, MyState.requestTwo);
    }

    @OnItemClick(R.id.lv_project)
    public void onItemClickProject(int position) {
        if (!type.equals("1")) {
            AddProjectBean bean = mAdapter.getItem(position);
            mAdapter.notifyDataSetChanged();
            Bundle bundle = new Bundle();
            bundle.putSerializable(AddLogActivity.PROJRCT_KEY, bean);
            setResult(0, new Intent().putExtras(bundle));
            finish();
        }
    }

    int typepostion = 0;//

    @OnItemClick(R.id.lv_type)
    public void onItemClickType(int position) {
        AddProjectTypeBean bean = mTypeAdapter.getList().get(position);
        mTypeAdapter.setSelectItem(bean.getName());
        typepostion = position;
//        mTypeAdapter.setFuwuXiangId(""+bean.getId());
        mAdapter.getList().clear();
        mAdapter.notifyDataSetChanged();
        mTypeAdapter.notifyDataSetChanged();
        requestProjectList(bean.getId());
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                List<AddProjectTypeBean> list = JSONArray.parseArray(data.toString(), AddProjectTypeBean.class);
                if (list != null && list.size() > 0) {
                    mTypeAdapter.setSelectItem(list.get(0).getName());
                    mTypeAdapter.setList(list);
                    mTypeAdapter.notifyDataSetChanged();
                    requestProjectList(list.get(0).getId());
                } else {
                    mTypeAdapter.getList().clear();
                    mTypeAdapter.notifyDataSetChanged();
                }
                break;
            case MyState.requestTwo:
                List<AddProjectBean> list2 = JSONArray.parseArray(data.toString(), AddProjectBean.class);
                if (list2 != null && list2.size() > 0) {
                    mAdapter.setList(list2);
                    mAdapter.notifyDataSetChanged();
                } else {
                    mAdapter.getList().clear();
                    mAdapter.notifyDataSetChanged();
                }
                break;
        }
    }
}
